home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / X11R4 / cmds / X / os / sprite / RCS / auth.c,v < prev    next >
Encoding:
Text File  |  1990-02-15  |  5.7 KB  |  258 lines

  1. head     1.1;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.1
  10. date     90.02.14.19.25.14;  author tve;  state Exp;
  11. branches ;
  12. next     ;
  13.  
  14.  
  15. desc
  16. @Original X11R4 distribution
  17. @
  18.  
  19.  
  20.  
  21. 1.1
  22. log
  23. @Initial revision
  24. @
  25. text
  26. @/*
  27.  * authorization hooks for the server
  28.  *
  29.  * $XConsortium: auth.c,v 1.8 89/12/13 14:42:27 keith Exp $
  30.  *
  31.  * Copyright 1988 Massachusetts Institute of Technology
  32.  *
  33.  * Permission to use, copy, modify, and distribute this software and its
  34.  * documentation for any purpose and without fee is hereby granted, provided
  35.  * that the above copyright notice appear in all copies and that both that
  36.  * copyright notice and this permission notice appear in supporting
  37.  * documentation, and that the name of M.I.T. not be used in advertising or
  38.  * publicity pertaining to distribution of the software without specific,
  39.  * written prior permission.  M.I.T. makes no representations about the
  40.  * suitability of this software for any purpose.  It is provided "as is"
  41.  * without express or implied warranty.
  42.  *
  43.  * Author:  Keith Packard, MIT X Consortium
  44.  */
  45.  
  46. # include   "X.h"
  47. # include   "Xauth.h"
  48. # include   "misc.h"
  49.  
  50. struct protocol {
  51.     unsigned short   name_length;
  52.     char    *name;
  53.     int     (*Add)();        /* new authorization data */
  54.     XID        (*Check)();        /* verify client authorization data */
  55.     int     (*Reset)();        /* delete all authorization data entries */
  56.     XID        (*ToID)();        /* convert cookie to ID */
  57.     int        (*FromID)();    /* convert ID to cookie */
  58.     int        (*Remove)();    /* remove a specific cookie */
  59. };
  60.  
  61. extern int  MitAddCookie ();
  62. extern XID  MitCheckCookie ();
  63. extern int  MitResetCookie ();
  64. extern XID  MitToID ();
  65. extern int  MitFromID (), MitRemoveCookie ();
  66.  
  67. #ifdef HASDES
  68. extern int  XdmAddCookie ();
  69. extern XID  XdmCheckCookie ();
  70. extern int  XdmResetCookie ();
  71. extern XID  XdmToID ();
  72. extern int  XdmFromID (), XdmRemoveCookie ();
  73. #endif
  74.  
  75. static struct protocol   protocols[] = {
  76. {   (unsigned short) 18,    "MIT-MAGIC-COOKIE-1",
  77.         MitAddCookie,    MitCheckCookie,    MitResetCookie,
  78.         MitToID,    MitFromID,    MitRemoveCookie,
  79. },
  80. #ifdef HASDES
  81. {   (unsigned short) 19,    "XDM-AUTHORIZATION-1",
  82.         XdmAddCookie,    XdmCheckCookie,    XdmResetCookie,
  83.         XdmToID,    XdmFromID,    XdmRemoveCookie,
  84. },
  85. #endif
  86. };
  87.  
  88. # define NUM_AUTHORIZATION  (sizeof (protocols) /\
  89.                  sizeof (struct protocol))
  90.  
  91. /*
  92.  * Initialize all classes of authorization by reading the
  93.  * specified authorization file
  94.  */
  95.  
  96. static char *authorization_file = (char *)NULL;
  97.  
  98. static int  AuthorizationIndex = 0;
  99. static Bool ShouldLoadAuth = TRUE;
  100.  
  101. InitAuthorization (file_name)
  102. char    *file_name;
  103. {
  104.     authorization_file = file_name;
  105. }
  106.  
  107. int
  108. LoadAuthorization ()
  109. {
  110.     FILE    *f;
  111.     Xauth   *auth;
  112.     int        i;
  113.     int        count = 0;
  114.  
  115.     ShouldLoadAuth = FALSE;
  116.     if (!authorization_file)
  117.     return 0;
  118.     f = fopen (authorization_file, "r");
  119.     if (!f)
  120.     return 0;
  121.     AuthorizationIndex = 0;
  122.     while (auth = XauReadAuth (f)) {
  123.     for (i = 0; i < NUM_AUTHORIZATION; i++) {
  124.         if (protocols[i].name_length == auth->name_length &&
  125.         bcmp (protocols[i].name, auth->name, (int) auth->name_length) == 0)
  126.         {
  127.         ++count;
  128.         (*protocols[i].Add) (auth->data_length, auth->data,
  129.                      ++AuthorizationIndex);
  130.         }
  131.     }
  132.     XauDisposeAuth (auth);
  133.     }
  134.     fclose (f);
  135.     return count;
  136. }
  137.  
  138. #ifdef XDMCP
  139. /*
  140.  * XdmcpInit calls this function to discover all authorization
  141.  * schemes supported by the display
  142.  */
  143. RegisterAuthorizations ()
  144. {
  145.     int        i;
  146.  
  147.     for (i = 0; i < NUM_AUTHORIZATION; i++)
  148.     XdmcpRegisterAuthorization (protocols[i].name,
  149.                     (int)protocols[i].name_length);
  150. }
  151. #endif
  152.  
  153. XID
  154. CheckAuthorization (name_length, name, data_length, data)
  155. unsigned short    name_length;
  156. char    *name;
  157. unsigned short    data_length;
  158. char    *data;
  159. {
  160.     int    i;
  161.  
  162.     if (ShouldLoadAuth)
  163.     {
  164.     if (!LoadAuthorization())
  165.         EnableLocalHost ();
  166.     }
  167.     if (name_length)
  168.     for (i = 0; i < NUM_AUTHORIZATION; i++) {
  169.         if (protocols[i].name_length == name_length &&
  170.         bcmp (protocols[i].name, name, (int) name_length) == 0)
  171.         {
  172.         return (*protocols[i].Check) (data_length, data);
  173.         }
  174.     }
  175.     return (XID) ~0L;
  176. }
  177.  
  178. ResetAuthorization ()
  179. {
  180.     int    i;
  181.  
  182.     for (i = 0; i < NUM_AUTHORIZATION; i++)
  183.     (*protocols[i].Reset)();
  184.     ShouldLoadAuth = TRUE;
  185. }
  186.  
  187. XID
  188. AuthorizationToID (name_length, name, data_length, data)
  189. {
  190.     int    i;
  191.  
  192.     for (i = 0; i < NUM_AUTHORIZATION; i++) {
  193.         if (protocols[i].name_length == name_length &&
  194.         bcmp (protocols[i].name, name, (int) name_length) == 0)
  195.         {
  196.         return (*protocols[i].ToID) (data_length, data);
  197.         }
  198.     }
  199.     return (XID) ~0L;
  200. }
  201.  
  202. AuthorizationFromID (id, name_lenp, namep, data_lenp, datap)
  203. XID id;
  204. unsigned short    *name_lenp;
  205. char    **namep;
  206. unsigned short    *data_lenp;
  207. char    **datap;
  208. {
  209.     int    i;
  210.  
  211.     for (i = 0; i < NUM_AUTHORIZATION; i++) {
  212.     if ((*protocols[i].FromID) (id, data_lenp, datap)) {
  213.         *name_lenp = protocols[i].name_length;
  214.         *namep = protocols[i].name;
  215.         return 1;
  216.     }
  217.     }
  218.     return 0;
  219. }
  220.  
  221. RemoveAuthorization (name_length, name, data_length, data)
  222. unsigned short    name_length;
  223. char    *name;
  224. unsigned short    data_length;
  225. char    *data;
  226. {
  227.     int    i;
  228.  
  229.     for (i = 0; i < NUM_AUTHORIZATION; i++) {
  230.         if (protocols[i].name_length == name_length &&
  231.         bcmp (protocols[i].name, name, (int) name_length) == 0)
  232.         {
  233.         return (*protocols[i].Remove) (data_length, data);
  234.         }
  235.     }
  236.     return 0;
  237. }
  238.  
  239. AddAuthorization (name_length, name, data_length, data)
  240. unsigned short    name_length;
  241. char    *name;
  242. unsigned short    data_length;
  243. char    *data;
  244. {
  245.     int    i;
  246.  
  247.     for (i = 0; i < NUM_AUTHORIZATION; i++) {
  248.         if (protocols[i].name_length == name_length &&
  249.         bcmp (protocols[i].name, name, (int) name_length) == 0)
  250.         {
  251.         return (*protocols[i].Add) (data_length, data,
  252.                     ++AuthorizationIndex);
  253.         }
  254.     }
  255.     return 0;
  256. }
  257. @
  258.